package com.kmxd.ams.app.system;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kmxd.ams.client.system.ISysDictTabsetSvc;
import com.kmxd.ams.client.system.dto.SysDictTabsetAddDTO;
import com.kmxd.ams.client.system.dto.SysDictTabsetEditDTO;
import com.kmxd.ams.client.system.dto.SysDictTabsetQueryDTO;
import com.kmxd.ams.client.system.vo.SysDictTabsetListVO;
import com.kmxd.ams.client.system.vo.SysDictTabsetVO;
import com.kmxd.ams.core.common.query.QueryGen;
import com.kmxd.ams.core.util.ConvertUtil;
import com.kmxd.ams.core.util.IdUtil;
import com.kmxd.ams.infra.system.entity.SysDictTabset;
import com.kmxd.ams.infra.system.mapper.SysDictTabsetMapper;
import java.util.List;
import java.util.Set;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 字典项 服务实现类
 *
 * @author ZuoLG
 * @since 2022-07-13
 */
@Service
@AllArgsConstructor
public class SysDictTabsetSvcImpl implements ISysDictTabsetSvc {

  private SysDictTabsetMapper sysDictTabsetMapper;

  @Override
  public Page<SysDictTabsetListVO> selectPage(Page reqPage, SysDictTabsetQueryDTO req) {
    LambdaQueryWrapper<SysDictTabset> queryWrapper =
        QueryGen.init(new QueryWrapper<SysDictTabset>(), req).lambda();
    Page<SysDictTabset> page = sysDictTabsetMapper.selectPage(reqPage, queryWrapper);
    Page<SysDictTabsetListVO> voPage = BeanUtil.copyProperties(page, Page.class);
    voPage.setRecords(BeanUtil.copyToList(page.getRecords(), SysDictTabsetListVO.class));
    return voPage;
  }

  @Override
  public List<SysDictTabsetListVO> list(SysDictTabsetQueryDTO req) {
    LambdaQueryWrapper<SysDictTabset> queryWrapper =
        QueryGen.init(new QueryWrapper<SysDictTabset>(), req).lambda();
    List<SysDictTabset> sysDictTabsets = sysDictTabsetMapper.selectList(queryWrapper);
    return ConvertUtil.toList(sysDictTabsets, SysDictTabsetListVO.class);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void add(SysDictTabsetAddDTO req) {
    SysDictTabset entity = BeanUtil.copyProperties(req, SysDictTabset.class);
    sysDictTabsetMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void edit(SysDictTabsetEditDTO req) {
    SysDictTabset entity = BeanUtil.copyProperties(req, SysDictTabset.class);
    sysDictTabsetMapper.updateById(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void remove(String ids) {
    Set<Long> idList = IdUtil.split(ids);
    sysDictTabsetMapper.deleteBatchIds(idList);
  }

  @Override
  public SysDictTabsetVO view(Long id) {
    SysDictTabset entity = sysDictTabsetMapper.selectById(id);
    return BeanUtil.copyProperties(entity, SysDictTabsetVO.class);
  }
}
